package org.jeecg.modules.kc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.kc.entity.KcGoodsType;
import org.jeecg.modules.kc.mapper.KcGoodsTypeMapper;
import org.jeecg.modules.kc.service.IKcGoodsTypeService;
import org.jeecg.modules.rooms.DTO.CesStockTypeDto;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: kc_goods_type
 * @Author: jeecg-boot
 * @Date:   2023-04-18
 * @Version: V1.0
 */
@Service
public class KcGoodsTypeServiceImpl extends ServiceImpl<KcGoodsTypeMapper, KcGoodsType> implements IKcGoodsTypeService {
    @Resource
    private KcGoodsTypeMapper kcGoodsTypeMapper;
    /**
     * 获取商品库存分类
     * @param hotelId
     * @return
     */
    public Result tree(String hotelId){
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //查询数据
        List<KcGoodsType> list = kcGoodsTypeMapper.selectList(Wrappers.<KcGoodsType>lambdaQuery()
                .eq(KcGoodsType::getHotelId,hotelId)
                .eq(KcGoodsType::getInvalid,false));
        list.forEach(v -> {
            nodeList.add(new TreeNode<>(v.getId(),v.getParentId(),v.getName(),0));
        });
        if(CollUtil.isEmpty(nodeList)) return Result.OK(new ArrayList<>());
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        return Result.OK(treeList);
    }

    public List<Tree<String>> treeList(String hotelId){
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //查询数据
        List<KcGoodsType> list = kcGoodsTypeMapper.selectList(Wrappers.<KcGoodsType>lambdaQuery()
                .eq(KcGoodsType::getHotelId,hotelId)
                .eq(KcGoodsType::getInvalid,false));
        list.forEach(v -> {
            nodeList.add(new TreeNode<>(v.getId(),v.getParentId(),v.getName(),0));
        });
        if(CollUtil.isEmpty(nodeList)) return new ArrayList<>();
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        return treeList;
    }


    /**
     * 获取顶级分类
     * @return
     */
    public Result getTopTypes(String hotelId){
        //查询数据
        List<KcGoodsType> list = kcGoodsTypeMapper.selectList(Wrappers.<KcGoodsType>lambdaQuery()
                .eq(KcGoodsType::getHotelId,hotelId)
                .eq(KcGoodsType::getState,true)
                .eq(KcGoodsType::getParentId,"0")
                .eq(KcGoodsType::getInvalid,false));
        return Result.OK(list);
    }

    /**
     * 创建分类
     * @param kcGoodsType
     * @return
     */
    public Result create(KcGoodsType kcGoodsType){
        LambdaQueryWrapper<KcGoodsType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KcGoodsType::getName,kcGoodsType.getName());
        queryWrapper.eq(KcGoodsType::getHotelId, kcGoodsType.getHotelId());
        queryWrapper.eq(KcGoodsType::getInvalid,true);

        KcGoodsType cesStockType = kcGoodsTypeMapper.selectOne(queryWrapper);
        if(!ObjectUtils.isEmpty(cesStockType)) return Result.error("已有相同名称的分类!");

        kcGoodsType.setInvalid(false);
        kcGoodsTypeMapper.insert(kcGoodsType);
        return Result.OK("保存成功!");
    }


    /**
     * 修改分类
     * @param kcGoodsType
     * @return
     */
    public Result modify(KcGoodsType kcGoodsType){
        KcGoodsType cesStockType = kcGoodsTypeMapper.selectById(kcGoodsType.getId());
        if(ObjectUtils.isEmpty(cesStockType)) return Result.error("数据未找到！");

        LambdaQueryWrapper<KcGoodsType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(KcGoodsType::getId,cesStockType.getId());
        queryWrapper.eq(KcGoodsType::getName,cesStockType.getName());
        queryWrapper.eq(KcGoodsType::getHotelId, cesStockType.getHotelId());
        queryWrapper.eq(KcGoodsType::getInvalid,false);

        KcGoodsType exit = kcGoodsTypeMapper.selectOne(queryWrapper);
        if(!ObjectUtils.isEmpty(exit)) return Result.error("已有相同名称的分类!");
        kcGoodsTypeMapper.updateById(kcGoodsType);
        return Result.OK("修改成功!");
    }


    public Result delete(KcGoodsType kcGoodsType){
        KcGoodsType cesStockType = kcGoodsTypeMapper.selectById(kcGoodsType.getId());
        if(ObjectUtils.isEmpty(cesStockType)) return Result.error("数据未找到！");
        cesStockType.setInvalid(true);
        kcGoodsTypeMapper.updateById(cesStockType);
        return Result.OK("删除成功!");
    }


    /**
     * 根据IDS 查询分类数据
     * @param ids
     * @return
     */
    public List<KcGoodsType> fetchByIds(List<String> ids){
        if(CollectionUtil.isEmpty(ids)) return new ArrayList<>();
        List<KcGoodsType> list = kcGoodsTypeMapper.selectList(Wrappers.<KcGoodsType>lambdaQuery().eq(KcGoodsType::getInvalid,false).in(KcGoodsType::getId,ids));

        return  list;
    }

}
